////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.orders.windows {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import plantool.data.Field;
	import plantool.display.windows.TableRowEditWindow;
	import plantool.display.windows.objects.WindowFieldRow;
	import plantool.display.windows.objects.WindowRowSeparator;
	import plantool.modules.orders.data.Order;
	import plantool.modules.orders.data.Ordermeta;
	import plantool.modules.orders.windows.objects.OrderWindowOrderRow;
	
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class OrderWindow extends TableRowEditWindow {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _order:Order						= new Order();
		private var _metaFields:Vector.<WindowFieldRow>	= new Vector.<WindowFieldRow>();
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public function get order():Order { return _order; }
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function OrderWindow() {
			//call super
			super(orderApi.orderTable, _order);
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		protected override function init():void {
			//call super init to create order-fields
			super.init();
			
			//go through all meta-fields
			for each (var field:Field in orderApi.orderMetaTable.fields) {
				//add a row for each field
				if (field.name != "order_id") {
					var row:WindowFieldRow 	= new WindowFieldRow(this, field, settings.getSetting(field.name).value);
					row.name				= field.name;
					row.width				= width;
					
					//add row to window
					_metaFields.push(addChild(row));
				}
			}
			
			//add seperator for the order-rows
			addChild(new WindowRowSeparator(this));
			
			//add orderrow-row
			addChild(new OrderWindowOrderRow(this));
		}
		
		protected override function save():void {
			//go through all fields
			var meta:Ordermeta = new Ordermeta();
			for each (var field:WindowFieldRow in _metaFields) {
				if (meta.hasOwnProperty(field.name)) {
					meta[field.name] = field.data;
				}
			}
			_order.meta = meta;
			
			orderApi.saveOrder(_order, null, null);
		}
	}
}